package com.nuliji.tools.shiro;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

/**
 * Created by gaojie on 2017/11/7.
 */
public class RedisManager {
    private JedisConnectionFactory factory;
    private static final Logger logger = LoggerFactory.getLogger(RedisManager.class);
    /**
     * 有效期
     */
    private int expire;
    public RedisManager() {
    }
    /**
     * get value from redis
     * @param key
     * @return
     */
    public byte[] get(byte[] key) {
        byte[] value = null;
        JedisConnection jedis = factory.getConnection();
        try {
//            logger.debug("get redis:{}", new String(key));
            value = jedis.get(key);
        } finally {
            jedis.close();
        }
        return value;
    }
    /**
     * set
     * @param key
     * @param value
     * @return
     */
    public byte[] set(byte[] key, byte[] value) {
        JedisConnection jedis = factory.getConnection();
        try {
//            logger.debug("set redis:{}", new String(key));
            jedis.set(key, value);
            if (this.expire != 0) {
                jedis.expire(key, this.expire);
            }
        } finally {
            jedis.close();
        }
        return value;
    }
    /**
     * set
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public byte[] set(byte[] key, byte[] value, int expire) {
        JedisConnection jedis = factory.getConnection();
        try {
//            logger.debug("set redis expire:{}", new String(key));
            jedis.set(key, value);
            if (expire != 0) {
//            logger.debug("set redis expire:{}", expire);
                jedis.expire(key, expire);
            }
        } finally {
            jedis.close();
        }
        return value;
    }
    /**
     * del
     * @param key
     */
    public void del(byte[] key) {
        JedisConnection jedis = factory.getConnection();
        try {
//            logger.debug("delete redis:{}", new String(key));
            jedis.del(key);
        } finally {
            jedis.close();
        }
    }
    /**
     * flush
     */
    public void flushDb() {
        JedisConnection jedis = factory.getConnection();
        try {
            logger.debug("flushDb redis");
            jedis.flushDb();
        } finally {
            jedis.close();
        }
    }
    /**
     * size
     */
    public Long dbSize() {
        Long dbSize = 0L;
        JedisConnection jedis = factory.getConnection();
        try {
            dbSize = jedis.dbSize();
        } finally {
            jedis.close();
        }
        return dbSize;
    }
    /**
     * keys
     * @param pattern
     * @return
     */
    public Set<byte[]> keys(String pattern) {
        Set<byte[]> keys = null;
        JedisConnection jedis = factory.getConnection();
        try {
//            logger.debug("keys redis:{}", pattern);
            keys = jedis.keys(pattern.getBytes());
        } finally {
            jedis.close();
        }
        return keys;
    }

    public void setFactory(JedisConnectionFactory factory) {
        this.factory = factory;
    }

    public void setExpire(int expire) {
        this.expire = expire;
    }
}
